//
//  MasterTableViewController.swift
//  YueJian
//
//  Created by Milo.o on 2017/11/13.
//  Copyright © 2017年 MapleStory. All rights reserved.
//

import UIKit
import Alamofire


class MasterTableViewController: UITableViewController {
    var mhPlayer: MHAVPlayerSDK?
    var dic:[String:AnyObject]?
    var masterId = ""
    override func viewDidLoad() {
        super.viewDidLoad()

        
        let id = UserDefaults.standard.string(forKey: "sessionId")
        let params = ["SessionID":id!,"id":masterId,"related":"1","map":"clubjw"]
        print(params)
        Alamofire.request(URL(string: GET_Master)!, method: .get, parameters: params, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
            print(response.result.value!)
            self.dic = response.result.value as? [String : AnyObject]
            self.tableView.reloadData()
        }
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
        tableView.estimatedRowHeight = UITableViewAutomaticDimension
        tableView.rowHeight = 180
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 3
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 1
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)

        if dic != nil {
            if indexPath.section == 0 {
                let str = dic?["thumb"] as! String
                let imageStr = "http://172.24.10.175/workout/Uploads/"+str
                let images = try! UIImage(data: Data(contentsOf: URL(string: imageStr)!))
                cell.backgroundView = UIImageView(image: images)
        
                
            } else if indexPath.section == 1{
                cell.backgroundView = nil
                cell.backgroundColor = UIColor.lightGray
                let name = dic?["realname"] as! String
                let str = dic?["introduction"] as! String
                cell.textLabel?.text = name + ":  " + str
                
            } else {

                mhPlayer = MHAVPlayerSDK(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.width / 2))
                mhPlayer?.mhPlayerURL = "http://172.24.10.175/workout/Uploads/tvideo/video/"+"zjs/mvc1.mp4"
                mhPlayer?.mhPlayerTitle = "大师视频"
                mhPlayer?.MHAVPlayerSDKDelegate = self
                mhPlayer?.mhLastTime = 50
                mhPlayer?.mhAutoOrient = false
                cell.addSubview(mhPlayer!)

            }
        }
        return cell
    }
    

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if section == 0 {
            return ""
            
        } else if section == 1{
          return "大师介绍"
            
        } else {
            if dic != nil {
                let city = dic?["city_name"] as! String
                let club = dic?["club_name"] as? String ?? ""
                return club + "   (\(city))"
            } else {
                return ""
            }
        }
    }
    
//    override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
//        if section == 0 {
//            return "大师介绍"
//        } else if dic != nil {
//            let city = dic?["city_name"] as! String
//            let club = dic?["club_name"] as? String ?? ""
//            return club + "   (\(city))"
//        } else {
//            return ""
//        }
//    }
    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
extension MasterTableViewController: MHAVPlayerSDKDelegate {
    
    func mhGoBack() {
        //            mhPlayer?.mhStopPlayer()
        //            self.dismiss(animated: true, completion: nil)
    }
    
    func mhNextPlayer() {
        mhPlayer?.mhPlayerURL = "http://baobab.wdjcdn.com/1455782903700jy.mp4"
        mhPlayer?.mhPlayerTitle = "第二部";
    }
    
}
